package com.purong.purong.fragment;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.adapter.MyScheduleAdapter;
import com.purong.purong.bean.CreditApplyLogBean;
import com.purong.purong.bean.CreditApplyLogListBean;
import com.purong.purong.bean.CreditCardBean;
import com.purong.purong.widget.LoadingView;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.views.ptr.PtrCustomHandler;
import in.srain.cube.views.ptr.PtrDefaultCustomHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrPuRongLayout;

/**
 * Created by wenjiezeng on 2017/9/28.
 */
public class MyScheduleFragment extends BaseFragment {
    private RecyclerView mRecyclerView;
    private PtrPuRongLayout mPtrFrame;
    private LoadingView loadView;
    private MyScheduleAdapter mAdapter;
    private List<CreditApplyLogBean> creditCardList=new ArrayList<>();
    private int page=1;


    @Override
    public int getRootViewId() {
        return R.layout.fragment_schedule_my;
    }

    public void initView(View v){
        loadView=(LoadingView)v.findViewById(R.id.loading_view);
        loadView.setNoResultToast("暂无记录","刷新");
        loadView.setLoadListener(new LoadingView.OnLoadListener() {
            @Override
            public void onReload() {
                getData(true);
            }

            @Override
            public void onRefresh() {
                getData(true);
            }
        });
        mRecyclerView=(RecyclerView)v.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false));
        mAdapter=new MyScheduleAdapter(getActivity());
        mAdapter.setData(creditCardList);
        mRecyclerView.setAdapter(mAdapter);
        mPtrFrame = (PtrPuRongLayout) v.findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame.setPtrHandler(new PtrCustomHandler() {
            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {
                if (creditCardList.size()==0){
                    return false;
                }else {
                    return PtrDefaultCustomHandler.checkContentCanBePulledUp(frame, content, footer);
                }

            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getData(false);

            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getData(true);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return (creditCardList.size()>0)?false:true;
            }
        });

    }

    @Override
    public void initData() {
        getData(true);
    }

    private void getData(boolean clear){
        Map<String, String> params = new LinkedHashMap<>();
        if (clear){
            page=1;
            loadView.loading();
            creditCardList.clear();
            mAdapter.notifyDataSetChanged();

        }
        params.put("page",page+"");
        Log.e("zwj","...page="+page);
        if (!request(Contant.CREDIT_APPLY_LOG, params, true)) {
            if (clear)
            loadView.fail();
            if (!hasDestroy())
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        if (mPtrFrame.isRefreshing()) {
                            mPtrFrame.refreshComplete();
                        }
                    }
                }, 100);
        }
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.CREDIT_APPLY_LOG)){
            if (mPtrFrame.isRefreshing()) {
                mPtrFrame.refreshComplete();
            }
            if (responseInfo.isRequestOk){
                try{
                    CreditApplyLogListBean bean= JSON.parseObject(responseInfo.content,CreditApplyLogListBean.class);
                    if (bean!=null&&bean.getCredit_apply_log_list()!=null){
                        creditCardList.addAll(bean.getCredit_apply_log_list());
                        if ( bean.getCredit_apply_log_list().size()>0)
                        page++;
                        mAdapter.notifyDataSetChanged();
                    }
                    if (creditCardList.size()==0){
                        loadView.noResultWithRefresh();
                    }else {
                        loadView.finish();
                    }

                }catch (Exception e){
                    if (page==1)
                        loadView.fail();
                }
            }else {
                if (page==1)
                    loadView.fail();
            }
        }
    }
}
